package com.wyp168.common.utils;

/**
 * @ClassName FeignUtil
 * @Title TODO
 * @Description TODO
 * @Author wyp168
 * @Date 2023/8/23 21:49
 */
import com.google.common.base.Strings;
import com.wyp168.common.exception.RRException;

import java.util.ArrayList;
import java.util.List;
public class FeignUtil {

    public static <T> T formatClass(R r, Class<T> tClass) {
        String jsonString = getResultJson(r);
        if (Strings.isNullOrEmpty(jsonString)) {
            return null;
        }
        return JsonUtil.parseObject(jsonString, tClass);
    }

    private static String getResultJson(R r) {
        if (0 != r.getCode()) {
            throw new RRException(r.get("msg").toString());
        }
        return JsonUtil.toJsonString(r.get("result"));
    }

    public static <T> List<T> formatListClass(R r, Class<T> tClass) {
        String jsonString = getResultJson(r);
        if (Strings.isNullOrEmpty(jsonString)) {
            return new ArrayList<>();
        }
        return JsonUtil.parseList(jsonString, tClass);
    }
}